package com.ftg.learn.chapter10.base01;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

/**
 * @author 布衣
 */
public class Play {

    public static void main(String[] args) {
        String path = "base/zong.xml";
        //把zong.xml里面的信息取到java
        SqlSession sqlSession = null;
        try {
            Reader reader = Resources.getResourceAsReader(path);

            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory sqlSessionFactory = builder.build(reader);

            sqlSession = sqlSessionFactory.openSession();

            Teachers mapper = sqlSession.getMapper(Teachers.class);
//            int a = mapper.save("张三丰","123");
//            System.out.println(a);
            TeacherEntity t = new TeacherEntity();
            t.setTeacherId(16);
            t.setTeacherName("张无艺");
            t.setDepartment("x");
            int n = mapper.updateForId(t,4);
            System.out.println(n);
            sqlSession.commit();
        } catch (IOException e) {
            if (sqlSession != null) {
                sqlSession.rollback();
            }
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

}
